﻿using System.Dynamic;
using Basic.FormDAL;
using Basic.FormModel;
using Basic.FormModel.AgentForm;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar.VirtualQuery;

namespace Basic.FormCollect.lmpl
{
    internal class AgentFormCollect : IAgentFormCollect
    {
        private readonly IAgentFormDAL _BasicDAL;

        public AgentFormCollect ( IAgentFormDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public ExpandoObject[] Gets ( QueryStruct query )
        {
            return this._BasicDAL.Gets(query);
        }
        public ExpandoObject[] Query ( QueryStruct query, IBasicPage paging, out int count )
        {
            return this._BasicDAL.Query(query, paging, out count);
        }
        public ExpandoObject Get ( QueryStruct query )
        {
            return this._BasicDAL.Get(query);
        }
        public ExpandoObject[] Query ( string table, List<IConditionalModel> where, string[] cols, IBasicPage paging, out int count )
        {
            paging.InitOrderBy(ConstDic.KeyColName, true);
            return this._BasicDAL.Query(table, where, cols, paging, out count);
        }
        public ExpandoObject[] Query ( string table, IBasicPage paging, out int count )
        {
            return this._BasicDAL.Query(table, paging, out count);
        }
        public void CreateTable ( string tableName, string show, AgentTableCol[] cols )
        {
            this._BasicDAL.CreateTable(tableName, show, cols);
        }
        public long Add ( string tableName, Dictionary<string, object> dic )
        {
            return this._BasicDAL.Add(tableName, dic);
        }
        public ExpandoObject[] Gets ( string tableName, long mainId, string[] cols )
        {
            List<IConditionalModel> where = new List<IConditionalModel>
            {
                new ConditionalModel
                {
                    FieldName=ConstDic.ForeignKeyColName,
                    FieldValue=mainId.ToString()
                }
            };
            return this._BasicDAL.Gets(tableName, where, cols);
        }
        public ExpandoObject[] Gets ( string tableName, string[] cols )
        {
            return this._BasicDAL.Gets(tableName, null, cols);
        }
        public Result[] Gets<Result> ( string tableName, long mainId, string col )
        {
            List<IConditionalModel> where = new List<IConditionalModel>
            {
                new ConditionalModel
                {
                    FieldName=ConstDic.ForeignKeyColName,
                    FieldValue=mainId.ToString()
                }
            };
            return this._BasicDAL.Gets<Result>(tableName, where, col);
        }
        public Result[] Gets<Result> ( string tableName, string col )
        {
            return this._BasicDAL.Gets<Result>(tableName, null, col);
        }
        public ExpandoObject Get ( string tableName, string linkType, long linkPk, params string[] col )
        {
            List<IConditionalModel> where = new List<IConditionalModel>
            {
                new ConditionalModel
                {
                    FieldName=ConstDic.LinkType,
                    FieldValue=linkType,
                },
                new ConditionalModel
                {
                    FieldName=ConstDic.LinkPk,
                    FieldValue=linkPk.ToString(),
                    CSharpTypeName="long"
                }
            };
            return this._BasicDAL.Get(tableName, where, col);
        }
        public ExpandoObject Get ( string tableName, string linkType, long linkPk )
        {
            List<IConditionalModel> where = new List<IConditionalModel>
            {
                new ConditionalModel
                {
                    FieldName=ConstDic.LinkType,
                    FieldValue=linkType,
                },
                new ConditionalModel
                {
                    FieldName=ConstDic.LinkPk,
                    FieldValue=linkPk.ToString(),
                    CSharpTypeName="long"
                }
            };
            return this._BasicDAL.Get(tableName, where);
        }
        public void CheckIsRepeat ( string table, string linkType, long linkPk )
        {
            List<IConditionalModel> where = new List<IConditionalModel>
            {
                new ConditionalModel
                {
                    FieldName=ConstDic.LinkType,
                    FieldValue=linkType,
                },
                new ConditionalModel
                {
                    FieldName=ConstDic.LinkPk,
                    FieldValue=linkPk.ToString(),
                    CSharpTypeName="long"
                }
            };
            if ( this._BasicDAL.IsExists(table, where) )
            {
                throw new ErrorException("form.query.tabe.data.repeat");
            }
        }
        public long Add ( TableAddData add )
        {
            this.CheckIsRepeat(add.TableName, add.LinkType, add.LinkPk);
            return this._BasicDAL.Add(add);
        }
        public ExpandoObject Get ( string tableName, List<IConditionalModel> where, List<SelectModel> selects )
        {
            return this._BasicDAL.Get(tableName, where, selects);
        }
        public ExpandoObject[] Gets ( string tableName, List<IConditionalModel> where, List<SelectModel> selects )
        {
            return this._BasicDAL.Gets(tableName, where, selects);
        }
        public ExpandoObject[] GroupBy ( string tableName, List<IConditionalModel> where, List<SelectModel> selects, List<GroupByModel> groupBy )
        {
            return this._BasicDAL.GroupBy(tableName, where, selects, groupBy);
        }
        public ExpandoObject Get ( string tableName, long id, string[] cols )
        {
            List<IConditionalModel> where = new List<IConditionalModel>
            {
                new ConditionalModel
                {
                    FieldName=ConstDic.KeyColName,
                    FieldValue=id.ToString(),
                }
            };
            return this._BasicDAL.Get(tableName, cols, where);
        }

        public ExpandoObject[] GetAll ( string tableName, long mainId, string[] cols )
        {
            List<IConditionalModel> where = new List<IConditionalModel>
            {
                new ConditionalModel
                {
                    FieldName=ConstDic.ForeignKeyColName,
                    FieldValue=mainId.ToString(),
                }
            };
            return this._BasicDAL.Gets(tableName, where, cols);
        }
    }
}
